<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.ionglobal.db.*" %>
<%@ page import="com.ionglobal.lib.*" %>
<%@ page import="com.ionglobal.misc.*" %>
<%@ page import="com.ionglobal.fms.*" %>
<%@ include file="../init_include.jsp" %>
<%
	String uploadpath=FmsEnv.getUploadPath();
	String uploadext=FmsEnv.getUploadFileExt();
	String disableext=FmsEnv.getDiableFileExt();

	Sysop sysop=(Sysop)session.getAttribute("sysop");
	if ( sysop!=null)	{
		FmsFile ff=FmsFile.getSysop(dbi,sysop.getId());
		if(ff.getId()>0 || SmsEnv.checkSuperUser(sysop.getName())) {
			disableext=ff.getDisableExt();
			uploadext=ff.getFileExt();
			uploadpath=ff.getFolder();
		}		
	} else {
		response.sendRedirect("../index.jsp");
		return;
	}	

	int fnLength=FmsEnv.getFileNameLength();
	String relativePath=request.getParameter("path");
	if(uploadpath==null || uploadpath.length()==0)
		uploadpath="";
	if(relativePath==null)
		 relativePath=uploadpath;		
	String module=request.getParameter("module");	
	if ( (module==null)||(module.length()<1)) 
		module="fms";
	String fileRoot=SysEnv.get("web.fileRoot");
	String rootUrl=SysEnv.get("web.fileUrl");	
	String completePath;
	if ( relativePath==null) 
		relativePath="";	
		
	relativePath=FileManager.chargeFilePath(relativePath);
	fileRoot=FileManager.chargeFilePath(fileRoot);			
	completePath=fileRoot+File.separator+relativePath;
%>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=<%=Language.getHTMLLanguageStr()%>">
<link rel="stylesheet" href="../css.css" type="text/css">
<script>
<!--
function doAction(actUrl)
{
	document.fileManage.action=actUrl;
	document.fileManage.submit();
}

function doRename(actUrl)
{
	if(document.fileManage.rename.value=="")
	{
		alert("<%=Language.getNameByCode("fms.file.rename.alert","Please input name first!")%>");
		document.fileManage.rename.focus();
		return false;
	}
	var fn='';
	var i=0;
	while (document.fileManage.ra[i])
	{
		if (document.fileManage.ra[i].checked)
		{
			fn=document.fileManage.ra[i].value;
			break;
		}
		i++;
	}
	if (fn!='')
	{
		document.fileManage.action=actUrl+'?ra='+fn;
		document.fileManage.submit();
	}
	else
		window.alert("<%=Language.getNameByCode("fms.alert","Please select one file or directory first!")%>");			
}

function confirmDel()
{	
	var i=0;
	var fn='';
	while (document.fileManage.ra[i]) {
		if (document.fileManage.ra[i].checked)
		{
			fn=document.fileManage.ra[i].value;
			break;
		}
		i++;
	}
	if (fn!='')	{
		var msg=<%=Language.getNameByCode("fms.alert.delete","'Will you delete '+fn+'?'")%>;
		if ( window.confirm(msg)) {
			document.fileManage.action='fmsFile_del.jsp';
			document.fileManage.submit();
		}
	}
	else
		window.alert("<%=Language.getNameByCode("fms.alert","Please select one file or directory first!")%>");			
}

function doCreate(actUrl)
{
	if(document.fileManage.createCate.value=="")
	{
		alert("<%=Language.getNameByCode("fms.directorycreate.alert","Please input directory name first!")%>");
		document.fileManage.createCate.focus();
		return false;
	}
	document.fileManage.action=actUrl;
	document.fileManage.submit();
}

function uploadCheck()
{
	if(document.uploadForm.file.value=="") {
		alert("<%=Language.getNameByCode("fms.upload.alert","Please select your locate file first!")%>");
		document.uploadForm.file.focus();
		return false;
	}		
	var fileext="<%=uploadext%>";
	var disablefileext="<%=disableext%>";
	fileext=fileext.toLowerCase();
	disablefileext=disablefileext.toLowerCase();	
	var filestr=document.uploadForm.file.value;
	filestr=filestr.toLowerCase();
	var intPos=0;
	var intPos1=0;
	var tempext="";
	var str=fileext;
	if(fileext!=null && fileext!="") {
		intPos=str.indexOf("|");		
		while(intPos!=-1) {
			tempext=str.substring(0,intPos);
			intPos1=filestr.indexOf(tempext);
			if(intPos1!=-1)
				return true;	
			str=str.substring(intPos+1);
			intPos=str.indexOf("|");					
		}
		tempext=str;
		intPos1=filestr.indexOf(tempext);
		if(intPos1!=-1)
				return true;		
		alert("<%=Language.getNameByCode("fms.upload.alert1","You cannot upload file with this type!")%>");
		return false;
	}	
	str=disablefileext;
	if(disablefileext!=null && disablefileext!="") {
		intPos=str.indexOf("|");		
		while(intPos!=-1) {
			tempext=str.substring(0,intPos);
			intPos1=filestr.indexOf(tempext);
			if(intPos1!=-1) {
			  alert("<%=Language.getNameByCode("fms.upload.alert1","You cannot upload file with this type!")%>");
				return false;	
			}
			str=str.substring(intPos+1);
			intPos=str.indexOf("|");					
		}
		tempext=str;
		intPos1=filestr.indexOf(tempext);
		if(intPos1!=-1) {
				alert("<%=Language.getNameByCode("fms.upload.alert1","You cannot upload file with this type!")%>");
				return false;
		}						
	}	
	uploadBegin();
	return true;
}

function uploadBegin(){
    if(uploadCheck==false){
	     return false;
	 }
	theFeats = "height=200,width=320,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no";
	strAppVersion = navigator.appVersion;
	if (document.uploadForm.file.value != "")
	{
	    if (strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4)
	    {	
				winstyle = "dialogWidth=320px; dialogHeight:200px; center:yes";
				window.showModelessDialog("progressbar.jsp",window,winstyle);
	    }
	}
}
//-->
</script>
</HEAD>
<body>
<form action=<%="\""+response.encodeURL("fmsFileUpload.jsp")+"\""%> enctype="multipart/form-data" method="post" name="uploadForm" id="uploadForm" onsubmit="return uploadCheck();">
<table width="100%" border="0" cellspacing="1" cellpadding="2" bgcolor="#666666">
<tr class="bgcolor_title_tr" >
<td>
<%
	String str="";
	str=Language.getNameByCode("sys.CurrentPos","Current Position")+":";
	str+=relativePath;
	out.println(str);
%>
&nbsp;&nbsp;
</td>
</tr>
<tr class="bgcolor_title_tr">
<td>
  <input type="hidden" name="filePath" value="<%=relativePath%>">
  <input type="hidden" name="rstUrl" value="fileManage.jsp">
  <input type="hidden" name="module" value="fms">
  <%=Language.getNameByCode("fms.fileupload","File Upload")%>
  <input type="file" name="file" maxlength="100">
  <input type="submit" name="Submit" value="<%=Language.getNameByCode("fms.upload","Upload")%>">
 </td>
</tr>
<tr class="bgcolor_title_tr" >
<td>
	<font color="#FF0000"><u><%=Language.getNameByCode("fms.uploadcomment","If your system isnot support double charset,please user filename with single charset!")%></u></font>
</td>
</tr>
</table>
</form>
<form method="post" action="" name="fileManage" >
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  	<td width="5%"><%=Language.getNameByCode("sys.select","Select")%></td>
    <td><%=Language.getNameByCode("fms.name","File Name")%></td>
    <td width="10%" align=center><%=Language.getNameByCode("fms.type","File Type")%></td>
<% if(SmsEnv.checkSuperUser(sysop.getName())) {%>    
    <td width="6%" align=center><%=Language.getNameByCode("sys.download","DownLoad")%></td>
<% }%>
    <td width="10%" align=center><%=Language.getNameByCode("fms.size","File Size")%></td>
  </tr>
<%

	int pageno=0,pagesize=0;	
	ArrayList array=FileManager.getFiles(completePath,uploadext,disableext,pageno,pagesize);
	str="";
	String filePath="";
	if(relativePath.length()>0)
	{
    File path=new File(completePath);
		String parentStr=path.getParent();
		if(parentStr.length()>=fileRoot.length()) {
			parentStr=parentStr.substring(fileRoot.length());
		%>
		<tr style="background-color:#<%=((0%2)==0)?"cccccc":"F7F7F7" %>">
    	<td width="5%"><input type="radio" name="ra" value="<%=parentStr %>" >
    	</td>
    	<td><img src="icon/folder.jpg" ><a href="<% out.print("fileManage.jsp?path="+parentStr); %>"><%=Language.getNameByCode("fms.parent","..")%></a></td>
    	<td width="10%" align=center><%=Language.getNameByCode("fms.directory","Directory")%></td>
    	<% if(SmsEnv.checkSuperUser(sysop.getName())) {%>
    	<td width="6%" align=center></td>
    	<% }%>
    	<td width="10%" align=center></td>
  		</tr>
  		<%}
  	}
  	if(array!=null) {
			for (int i=0;i<array.size();i++) {	
				FileItem fi=(FileItem)array.get(i);
				String childPath=relativePath+File.separator+fi.getFileName();				
				String displayname=FileManager.DisplayEncode(fi.getFileName());
				%>
		<tr style="background-color:#<%=(((i+1)%2)==0)?"cccccc":"F7F7F7" %>">
    	<td width="5%"><input type="radio" name="ra" value="<%=fi.getFileName()%>"></td>
    	<td><img src="<%=fi.getIcon()%>" >
    	<%
    		if(fi.isDirectory()) {    		
					childPath=FileManager.LinkEncode(childPath);
    			if(displayname.length()>fnLength)
						out.print("<a href=\"fileManage.jsp?path="+childPath+"\" title=\""+displayname+"\">"+displayname.substring(0,fnLength)+"</a>");
					else
						out.print("<a href=\"fileManage.jsp?path="+childPath+"\">"+displayname+"</a>");					
    		} else {
					String url=relativePath.replace('\\','/');
					String fileRef=rootUrl+"/"+url+"/"+fi.getFileName();;
					while (fileRef.indexOf("//")!=-1)
					fileRef=Utilities.strReplace(fileRef,"//","/");
					fileRef=FileManager.LinkEncode(fileRef);
    			if(displayname.length()>fnLength)
						out.print("<a href=\""+fileRef+"\" target=\"_blank\" title=\""+displayname+"\">"+displayname.substring(0,fnLength)+"</a>");
					else
						out.print("<a href=\""+fileRef+"\" target=\"_blank\">"+displayname+"</a>");
			}%>				
    	</td>
    	<td width="10%" align=center>
    		<% if(fi.isDirectory()) 
    					out.print(Language.getNameByCode("fms.directory","Directory"));
    			 else
    					out.print(fi.getType());    	    							 
    		%>
    	</td>
    	<% if(SmsEnv.checkSuperUser(sysop.getName())) {%>
    	<td width="6%" align=center>
    	<%
    		if(!fi.isDirectory()) {    		%>
    		<a href="fmsdownFile.jsp?link=<%=(completePath+File.separator+displayname)%>"><%=Language.getNameByCode("sys.download","DownLoad")%></a>
    	<% }%>
    	</td>
    	<% }%>
    	<td width="10%" align=right><%=(fi.isDirectory()?"":fi.getSize()+"k")%></td>
  		</tr>
  		<%
  	}
	}
%>
  </table>
	
	<table width="100%" border="0">
<% 	if (array!=null && array.size()>0)
	{%>
	    <tr>
	      <td width="17%">
	        <input type="button" name="Submit4" value="<%=Language.getNameByCode("sys.Delete","Delete")%>" onClick="confirmDel()">
	      </td>
	      <td width="24%">(<i>*<%=Language.getNameByCode("fms.deletecommnet","Delete file or directory")%></i>)</td>
	      <td width="59%">
        <input type="button" name="Submit5" value="<%=Language.getNameByCode("sys.Edit","Edit")%>" onClick="doAction('fmsFile_edit.jsp')">
      </td>
	    </tr>
	    <tr>
	      <td width="17%"><%=Language.getNameByCode("fms.renameto","Rename To")%>:</td>
	      <td width="24%"> 
	        <input type="text" name="rename">
	      </td>
	      <td width="59%"> 
	        <input type="button" name="Submit3" value="<%=Language.getNameByCode("fms.rename","Rename")%>" onClick="doRename('fmsFile_rename.jsp')">
	        (<i>*<%=Language.getNameByCode("fms.rename.comment","Rename file or directory.")%></i>) </td>
	    </tr>
	 <%}
%>   
    <tr>
      <td width="17%"><%=Language.getNameByCode("fms.create.subdirectory","Create sub-directory")%>:</td>
      <td width="24%"> 
        <input type="text" name="createCate">
      </td>
      <td width="59%"> 
        <input type="button" name="Submit2" value="<%=Language.getNameByCode("fms.create","Create")%>" onClick="doCreate('fmsFile_mkdir.jsp')">
        (<i><%=Language.getNameByCode("fms.create.comment","Create sub-directory in current directory!")%></i>) </td>
    </tr>
  </table>
<input type="hidden" name="path" value="<%=relativePath%>">
<input type="hidden" name="module" value="fms">
</form>
</body>
</HTML>
<%@ include file="../init_free.jsp" %>